<template>

</template>

<script>
	import {
		formatNumber,
		formatDateThis,
		getUnixTime
	} from "@/common/util/dateUtil.js"

	export default {
		components: {

		},
		data() {
			return {

			}
		},
		onLoad() {

		},
		methods: {
			tableToExcel(headers, tableData, userName) {
				//列标题
				let headStr = '<tr>'
				for (let item of headers) {
					headStr += '<td>' + item.label + '</td>';
				}
				headStr = headStr + '</tr>'
				let worksheet = "sheet1";
				//循环遍历，每行加入tr标签，每个单元格加td标签
				let bodyStr = "";
				for (let i = 0; i < tableData.length; i++) {
					bodyStr += '<tr>';
					for (let item of headers) {
						//增加\t为了不让表格显示科学计数法或者其他格式
						bodyStr += `<td>${ tableData[i][item.key] + '\t'}</td>`;
					}
					bodyStr += '</tr>';
				}
				//下载的表格模板数据
				let template =
					`<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" 		xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="utf-8" />
 		<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>  <x:ExcelWorksheet><x:Name>Sheet1</x:Name><x:WorksheetOptions><x:DisplayGridlines/>     </x:WorksheetOptions> 		</x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>
		<table>${headStr}${bodyStr}</table>
		</body></html>`;
				//下载模板
				this.exportFile(template, "smartboot", userName);
			},

			// 导出文件到手机 fileData:要写入到文件的数据，返回参数为文档路径
			exportFile(fileData, documentName = "smartboot", userName) {
				/*
				PRIVATE_DOC: 应用私有文档目录常量
				PUBLIC_DOCUMENTS: 程序公用文档目录常量
				*/
				plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, (fs) => {

					let rootObj = fs.root;
					let fullPath = rootObj.fullPath;
					// let reader = rootObj.createReader();
					// console.log(reader);
					// reader.readEntries((res)=>{
					//     console.log(res); //这里拿到了该目录下所有直接文件和目录
					// },(err)=>{console.log(err);})

					console.log("开始导出数据********" , userName);
					// 创建文件夹
					rootObj.getDirectory(documentName, {
						create: true
					}, (dirEntry) => {
						//获取当前的年月继续创建文件夹
						let date = new Date();
						let year = date.getFullYear();
						let month = date.getMonth() + 1;
						dirEntry.getDirectory(`${year}-${month}`, {
							create: true
						}, (dirEntry2) => {
							// 创建文件,防止重名
							let fileName = getUnixTime(formatDateThis(new Date()));
							if (userName) {
								fileName = userName + fileName
							}
							console.log(fileName);
							dirEntry2.getFile(`${fileName}.xls`, {
								create: true
							}, (fileEntry) => {
								fileEntry.createWriter((writer) => {
									writer.onwritestart = (e) => {
										uni.showLoading({
											title: "正在导出",
											mask: true
										})
									}

									//  /storage/emulated/0指的就是系统路径
									let pathStr = fullPath.replace("/storage/emulated/0", "");
									writer.onwrite = (e) => {
										// 成功导出数据;
										uni.hideLoading();
										setTimeout(() => {
											uni.showToast({
												title: "成功导出",
												icon: "success"
											})
											this.$emit("successTip", `${pathStr}${documentName}/${year}-${month}/${fileName}.xls`)
										}, 500)
										writer.close();
									};

									// 写入内容;
									writer.write(fileData);


								}, (e) => {
									console.log(e.message);
								});
							});
						})

					});

				});
			}
		}



	}
</script>
